﻿
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.IO;
using System.Collections;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;

using Swaf;
using Swaf.BizRule;
using Swaf.BizObj;
using Swaf.Container;

using MediaCache;
using Syrinx;

using Splicer.Renderer;
using Splicer.Timeline;

namespace MediaCacheAdmin
{
	public class VideoProcessingBizRules : Syrinx.BaseBizRules
	{
		public void getVideoSnapshots(IApplication app, IRuleContext context, string videoFileName, double startOffset, int numShots)
		{
			FileInfo vid = new FileInfo(videoFileName);
			DirectoryInfo framesDir = new DirectoryInfo(Cmn.pathCombine(vid.DirectoryName, "frames"));
			Directory.CreateDirectory(framesDir.FullName);
			framesDir.Attributes = framesDir.Attributes | FileAttributes.Hidden;
			framesDir = new DirectoryInfo(Cmn.pathCombine(vid.DirectoryName, "frames", vid.Name + ".frames"));
			Directory.CreateDirectory(framesDir.FullName);
			framesDir.Attributes = framesDir.Attributes | FileAttributes.Hidden;

			using (var timeline = new DefaultTimeline())
			{
				timeline.AddVideoGroup(24, 640, 480).AddTrack(); // we want 320x240 sized images
				IClip vClip = timeline.AddVideo(videoFileName);
				double duration = vClip.Duration;
				double dist = duration / numShots;
				double[] clipPositions = new double[numShots];
				for(int pos = 0; pos < numShots; pos++)
					clipPositions[pos] = startOffset + (dist * pos);

				ImagesToDiskParticipant participant = new ImagesToDiskParticipant(24, 640, 480, framesDir.FullName, clipPositions);

				using (var render = new NullRenderer(timeline, null, new ICallbackParticipant[] { participant }))
				{
					render.Render();
				}
			}
		}
	}
}
